//
//  ViewController.m
//  DesignPatternDemo
//
//  Created by xiulian.yin on 16/3/3.
//  Copyright © 2016年 YIN.INC. All rights reserved.
//

#import "ViewController.h"
#import "DecoratorViewController.h"
#import "ChainOfResponsibilityViewController.h"
#import "TemplateMethodViewController.h"
#import "StrategyViewController.h"
#import "CoordinatingController.h"
#import "FlyweightViewController.h"
#import "AbstractViewController.h"
#import "BuilderViewController.h"
#import "BridgeViewController.h"
#import "EmulatorViewController.h"

#define CellId          @"cellId"

typedef enum DesignPatternSection
{
    eDesignPatternSectionCreateObj = 0,// 对象创建
    eDesignPatternSectionInterfaceAdapt, // 接口适配
    eDesignPatternSectionObjDismissContact, // 对象去耦
    eDesignPatternSectionAbstractSet,       // 抽象集合
    eDesignPatternSectionActivityExtention, // 行为扩展
    eDesignPatternSectionAlgriomPackage,  // 算法封装
    eDesignPatternSectionPerformanceAndObjVisit,    // 性能与对象访问
    eDesignPatternSectionObjStatus,         // 对象状态
    
    eDesignPatternSectionCount
}eDesignPatternSection;
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSArray *arrayGroups;
@property (nonatomic, retain) NSArray *arrayPatterns;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setTitle:@"IOS设计模式Demo"];
    
    _arrayGroups = [[NSArray alloc] initWithObjects:@"对象创建",@"接口适配",@"对象去耦",@"抽象集合",@"行为扩展",@"算法封装",@"性能与对象访问",@"对象状态", nil];
    NSArray *arrayCreateObjs = @[@"原型",@"工厂方法",@"抽象工厂",@"生成器",@"单例"];
    NSArray *arrayInterfaceAdapte = @[@"适配器",@"桥接",@"外观"];
    NSArray *arrayObjDissmissContact =@[@"中介者",@"观察者"];
    NSArray *arrayAbstracSet = @[@"组合",@"迭代器"];
    NSArray *arrayActionExtentions = @[@"访问者",@"装饰",@"责任链"];
    NSArray *arrayAlgorithms = @[@"模板方法",@"策略",@"命令"];
    NSArray *arrayPerformances = @[@"享元",@"代理"];
    NSArray *arrayObjStatus = @[@"备忘录"];
    
    _arrayPatterns = [NSArray arrayWithObjects:arrayCreateObjs,arrayInterfaceAdapte,arrayObjDissmissContact,arrayAbstracSet,arrayActionExtentions,arrayAlgorithms,arrayPerformances,arrayObjStatus, nil];
    
    UITableView *mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [mainTableView setDelegate:self];
    [mainTableView setDataSource:self];
    
    [self.view addSubview:mainTableView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableViewDelegate and datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return eDesignPatternSectionCount;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *curGroup = [_arrayPatterns objectAtIndex:section];
    return [curGroup count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_arrayGroups objectAtIndex:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
    }
    
    NSArray *curGroup = [_arrayPatterns objectAtIndex:indexPath.section];
    [cell.textLabel setText:[NSString stringWithFormat:@"   *%@",[curGroup objectAtIndex:indexPath.row]]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == eDesignPatternSectionCreateObj)
    {
        switch (indexPath.row)
        {
            case 0:
                // 原型模式
                [self onclickCommonDemo];
                break;
            case 1:
            {
                // 工厂方法-PaperCanvasView,ClothCanvasView,CanvasView
                [self onclickCommonDemo];
            }
                break;
            case 2:
            {
                // 抽象工厂
                AbstractViewController *abstractVC = [[AbstractViewController alloc] init];
                [self.navigationController pushViewController:abstractVC animated:YES];
            }
                break;
            case 3:
            {
                // 生成器
                BuilderViewController *builderVC = [[BuilderViewController alloc] init];
                [self.navigationController pushViewController:builderVC animated:YES];
            }
                break;
            case 4:
            {
                // 单例
                // [CoordinatingController sharedInstance];
                [self onclickCommonDemo];
            }
                break;
                
            default:
                break;
        }
    }
    else if (indexPath.section ==  eDesignPatternSectionInterfaceAdapt)
    {
        // 接口适配
        switch (indexPath.row)
        {
            case 0:
                //适配器
            {
                [self onclickCommonDemo];
            }
                break;
            case 1:
            {
                // 桥接
                BridgeViewController *bridgeCtrl = [[BridgeViewController alloc] init];
                [self.navigationController pushViewController:bridgeCtrl animated:YES];
            }
                break;
            case 2:
            {
                // 外观
                // scribbleManager saveScribble:scribble thumbnail:canvasViewImage];
                [self onclickCommonDemo];
            }
            default:
                break;
        }
    }
    else if (indexPath.section == eDesignPatternSectionObjDismissContact)
    {
        // 对象去耦
        switch (indexPath.row)
        {
            case 0:
            {
                // 中介者
                // 见CoordinatingController requestViewChangeByObject
                [self onclickCommonDemo];
            }
                break;
            case 1:
            {
                // 观察者
                // 见observeValueForKeyPath
                [self onclickCommonDemo];
            }
                
            default:
                break;
        }
    }
    else if (indexPath.section == eDesignPatternSectionAbstractSet)
    {
        // 抽象集合
        switch (indexPath.row)
        {
            case 0:
            {
                // 组合模式
                // 见Mark对象
                [self onclickCommonDemo];
            }
                break;
            case 1:
            {
                // 迭代器
                EmulatorViewController *emulatorVC = [[EmulatorViewController alloc] init];
                [self.navigationController pushViewController:emulatorVC animated:YES];
            }
                break;
            default:
                break;
        }
    }
    else if (indexPath.section == eDesignPatternSectionActivityExtention)
    {
        // 行为扩展
        switch (indexPath.row)
        {
            case 0:
            {
                // 访问者
            }
                break;
            case 1:
            {
                // 装饰
                DecoratorViewController *decoratorCtrl = [[DecoratorViewController alloc] init];
                [self.navigationController pushViewController:decoratorCtrl animated:YES];
            }
                break;
            case 2:
            {
                // 责任链
                ChainOfResponsibilityViewController *chainVC = [[ChainOfResponsibilityViewController alloc] init];
                [self.navigationController pushViewController:chainVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    }
    else if (indexPath.section == eDesignPatternSectionAlgriomPackage)
    {
        // 算法封装
        switch (indexPath.row)
        {
            case 0:
            {
                // 模板方法
                TemplateMethodViewController *templateVC = [[TemplateMethodViewController alloc] init];
                [self.navigationController pushViewController:templateVC animated:YES];
                
            }
                break;
            case 1:
            {
                // 策略模式
                StrategyViewController *stragegyVC = [[StrategyViewController alloc] init];
                [self.navigationController pushViewController:stragegyVC animated:YES];
            }
                break;
            case 2:
            {
                // 命令模式
                [self onclickCommonDemo];
            }
                break;
                
            default:
                break;
        }
    }
    else if (indexPath.section == eDesignPatternSectionPerformanceAndObjVisit)
    {
        // 性能与对象访问
        switch (indexPath.row)
        {
            case 0:
            {
                // 享元
                FlyweightViewController *shareUnitVC = [[FlyweightViewController alloc] init];
                [self.navigationController pushViewController:shareUnitVC animated:YES];
            }
                break;
            case 1:
            {
                // 代理
                [self onclickCommonDemo];
            }
                break;
            default:
                break;
        }
    }
    else if (indexPath.section == eDesignPatternSectionObjStatus)
    {
        switch (indexPath.row)
        {
            case 0:
                // 备忘录
            {
                [self onclickCommonDemo];
            }
                break;
                
            default:
                break;
        }
    }
    
}

-(void)onclickCommonDemo
{
    CoordinatingController *coordinatingController = [CoordinatingController sharedInstance];
    [self.navigationController pushViewController:[coordinatingController activeViewController] animated:YES];
}
@end
